<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright  Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Coupon codes grid
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Silk_Zgb_Block_Adminhtml_Creditmemo_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('creditmemoGrid');
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setUseAjax(true);
    }

    /**
     * Prepare collection for grid
     *
     * @return Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareCollection()
    {
        //echo $storeId;
        /*
        $collection = Mage::getModel('catalog/product')->getCollection()->setStoreId($storeId)
            ->addAttributeToSelect('sku');

        $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
            //$collection->addStoreFilter($store);
        $collection->joinAttribute(
            'name',
            'catalog_product/name',
            'entity_id',
            null,
            'inner',
            $adminStore
        );
        $collection->joinField('qty',
                'zgb/inventory',
                'qty',
                'product_id=entity_id',
                '{{table}}.website_id='.$websiteId,
                'left');

        $collection->joinField('order_id',
                'zgb/inventory',
                'order_id',
                'product_id=entity_id',
                '{{table}}.website_id='.$websiteId,
                'left');

        $collection->joinField('created_at',
                'zgb/inventory',
                'created_at',
                'product_id=entity_id',
                '{{table}}.website_id='.$websiteId,
                'left');

        $collection->joinField('comment',
                'zgb/inventory',
                'comment',
                'product_id=entity_id',
                '{{table}}.website_id='.$websiteId,
                'left');
        */
        //echo $collection->getSelect();
        //$collection = Mage::getResourceModel('zgb/inventory_collection')
        //    ->setOrder('id','desc');
        $collection = Mage::getResourceModel('sales/order_creditmemo_item_collection');
        $collection->addFieldToSelect(array('*'));
        $select = $collection->getSelect();
        $select->joinLeft(array('grid'=>'sales_flat_creditmemo_grid'),"main_table.parent_id=grid.entity_id",array('increment_id'=>'grid.increment_id',
            'order_id'=>'grid.order_increment_id',
            'created_at'=>'grid.created_at'));

        $select->joinLeft(array('order' => 'sales_flat_order'), "order.entity_id=grid.order_id", array('store_name'=>'order.customer_lastname'));

        // $name  = Mage::getResourceModel('catalog/product')->getAttribute('name');
        // $tablename = $name->getBackend()->getTable();
        // $attributeId = $name->getAttributeId();

        // $select->joinLeft(array('pn' => $tablename), "pn.entity_id=main_table.product_id and store_id=0 and pn.attribute_id='{$attributeId}'", array('name'=>'pn.value'));

        // $select->joinLeft(array('psku' => 'catalog_product_entity'), "psku.entity_id=main_table.product_id", array('sku'=>'psku.sku'));

        // $collection->addFilterToMap('name', 'pn.value');
        // $collection->addFilterToMap('sku', 'psku.sku');
        $collection->addFilterToMap('created_at', 'grid.created_at');

        // if($warehouse && $warehouse->getId())
        // {
        //     $collection->addFieldToFilter('website_id', array('eq'=>$websiteId));
        //     $collection->addFieldToFilter('product_id', array('in'=>$ids));
        // }
        // $collection->setOrder('id','desc');
        //$collection->addAttributeToFilter('sku',array('eq'=>'KSF019'));
        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
        if($websiteId){
            $storeId = Mage::getModel('core/website')->load($websiteId)->getDefaultStore()->getId();
            $collection->addAttributeToFilter("order.store_id",$storeId);
        }
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    public function getItemCollection()
    {
        $this->_prepareCollection();
        return $this->setCollection();
    }

    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    /**
     * Define grid columns
     *
     * @return Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareColumns()
    {
        $this->addColumn('order_id', array(
            'header' => Mage::helper('zgb')->__('订单号'),
            'index'  => 'order_id',
            'width' => '10'
        ));

        $this->addColumn('created_at', array(
            'header' => Mage::helper('zgb')->__('退款时间'),
            'index'  => 'created_at',
            'type'  => 'datetime',
            'gmtoffset'=>true
        ));

        $this->addColumn('name', array(
            'header' => Mage::helper('zgb')->__('Name'),
            'index'  => 'name',
        ));

        $this->addColumn('sku', array(
            'header' => Mage::helper('zgb')->__('SKU'),
            'index'  => 'sku',
        ));

        $this->addColumn('qty', array(
            'header' => Mage::helper('zgb')->__('Qty'),
            'index'  => 'qty',
            'width' => '10'
        ));

        $store = $this->_getStore();

        $this->addColumn('price', array(
            'header' => Mage::helper('zgb')->__('Price'),
            'index'  => 'price',
            'type'  => 'price',
            'currency_code' => $store->getBaseCurrency()->getCode(),
            'width' => '10'
        ));

        $this->addColumn('row_total', array(
            'header' => Mage::helper('zgb')->__('小计'),
            'index'  => 'row_total',
            'type'  => 'price',
            'currency_code' => $store->getBaseCurrency()->getCode(),
            'width' => '10'
        ));

        $this->addColumn('store_name', array(
            'header' => Mage::helper('zgb')->__('店铺名'),
            'index'  => 'store_name',
        ));


        // $this->addColumn('operation', array(
        //     'header'   => Mage::helper('zgb')->__('操作'),
        //     'index'    => 'operation',
        //     'type'     => 'options',
        //     'options'  => array(
        //         '1'=>Mage::helper('adminhtml')->__('入库'),
        //         '2'=>Mage::helper('adminhtml')->__('出库')
        //     ),
        // ));

        // $this->addColumn('order_id', array(
        //     'header' => Mage::helper('zgb')->__('订单号'),
        //     'index'  => 'order_id',
        //     'width' => '20'
        // ));

        // $this->addColumn('comment', array(
        //     'header' => Mage::helper('zgb')->__('备注'),
        //     'index'  => 'comment',
        // ));

        // $this->addColumn('created_at', array(
        //     'header' => Mage::helper('zgb')->__('Created On'),
        //     'index'  => 'created_at',
        //     'type'   => 'datetime',
        //     'align'  => 'center',
        //     'width'  => '200'
        // ));

        //$this->addExportType('*/*/exportCouponsCsv', Mage::helper('customer')->__('CSV'));
        $this->addExportType('*/*/export', Mage::helper('customer')->__('Excel'));
        return parent::_prepareColumns();
    }

    /**
     * Configure grid mass actions
     *
     * @return Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Coupons_Grid
     */
    protected function _prepareMassaction()
    {
        // $this->setMassactionIdField('coupon_id');
        // $this->getMassactionBlock()->setFormFieldName('ids');
        // $this->getMassactionBlock()->setUseAjax(true);
        // $this->getMassactionBlock()->setHideFormElement(true);

        // $this->getMassactionBlock()->addItem('export', array(
        //      'label'=> Mage::helper('adminhtml')->__('导出'),
        //      'url'  => $this->getUrl('*/*/export', array('_current' => true)),
        //      'complete' => 'index'
        // ));

        return $this;
    }

    /**
     * Get grid url
     *
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('admin_zgb/adminhtml_creditmemo/grid', array('_current'=> true));
    }
}
